import {
  getReportListApi,
  updateReportListApi,
  deleteReportListApi,
  getUpdateReportListApi,
  addReportListApi,
  cancelSharingReportApi,
  sharingReportApi,
  getReportUserApi,
  getSharedKeyApi,
  addSharedKeyApi,
  deleteSharedKeyApi,
  getFollowingReportsApi,
  getFollowingStatusApi,
  followReportApi,
  unfollowReportApi

} from '@/api/customReportManage'

import { queryReportDetail, queryFilterValue, queryDadasetData } from '@/api/query_report'
import storage from "store";
import {REFRESH_TOKEN} from "@/store/mutation-types";

const reportManage = {
  namespaced: true,
  state: {
    // 报表管理列表
    reportList: [],
    // 分页
    currentConfig:{
      current : 1,
      pageSize: 10,
      total: 50,
      pageSizeOptions: ['10', '30', '50', '100'],
      showQuickJumper: true,
      showSizeChanger: true,
      showTotal: (total) => {
        let pageConfig = reportManage.state.currentConfig
        return `共 ${total} 条记录 第${pageConfig.current} / ${Math.ceil(total/pageConfig.pageSize)} 页`
      }
    },
    // 排序
    order: null,
    orderBy: null,
    // 搜索字段
    search: {
      name: null,
      remark: null
    },
    datail: {},
    dataset: []
  },
  mutations: {
    SET_LIST: (state, list) => {
      state.reportList = list
    },
    SET_TOTAL: (state, total) => {
      state.currentConfig.total = total
    },
    SET_CURRENT: (state, current) => {
      state.currentConfig.current = current
    },
    SET_PAGES: (state, pages) => {
      state.currentConfig.pageSize = pages
    },
    SET_ORDER: (state, {order, orderBy}) => {
      state.order = order
      state.orderBy = orderBy
    },
    SET_SEARCH: (state, search) => {
      state.search = {
        ...search
      }
    },
    SET_REPORT: (state, report) => {
      const current_report = state.report
      state.report = {...current_report, ...report}
    },
    SET_DATAIL: (state, datail) => {
      state.datail = datail
    },
    SET_DATASET: (state, dataset) => {
      state.dataset = dataset
    }
  },
  actions: {
    // 获取select参数
    query_filter_value ({commit}, id) {
      return new Promise((resove, reject) => {
        queryFilterValue(id)
          .then(res => {
            if (res.code == 0) {
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取报表数据集
    query_dadaset_data ({ commit }, id) {
      return new Promise((resove, reject) => {
        queryDadasetData(id)
          .then(res => {
            if (res.code == 0) {
              commit('SET_DATASET', res.data)
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 获取报表详情
    query_report_detail ({ commit }, id) {
      return new Promise((resove, reject) => {
        queryReportDetail({...id})
          .then(res => {
            if (res.code == 0) {
              commit('SET_DATAIL', res.data)
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getReportList ( { state, commit } ) {
      const pageSize = state.currentConfig.pageSize,
            page = state.currentConfig.current,
            order = state.order,
            orderBy = state.orderBy,
            name = state.search.name || null,
            remark = state.search.remark || null
      return new Promise((resove, reject) => {
        getReportListApi({ page, pageSize, order, orderBy, name, remark })
          .then(res => {
            if (res.code ==0) {
              const { data } = res
              commit('SET_LIST', data.data)
              commit('SET_TOTAL', data.total)
              commit('SET_CURRENT', data.current)
              commit('SET_PAGES', data.size)
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    addReportList ({ commit }, form) {
      return new Promise((resove, reject) => {
        addReportListApi({...form})
          .then(res => {
            if (res.code == 0) {
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    updateReportList ({ commit }, form) {
      return new Promise((resove, reject) => {
        updateReportListApi({...form})
          .then(res => {
            if (res.code == 0) {
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getUpdateReportList ({ commit }, id) {
      return new Promise((resove, reject) => {
        getUpdateReportListApi(id)
          .then(res => {
            if (res.code == 0) {
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    deleteReportList ({ commit }, id) {
      return new Promise((resove, reject) => {
        deleteReportListApi({ id })
          .then(res => {
            if (res.code == 0) {
              resove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    sharingReport ({ commit }, param) {
      const reportId = param.reportId
      return new Promise((resolve, reject) => {
        sharingReportApi(reportId, param)
          .then(res => {
            if (res.code === 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    cancelSharingReport ({ commit }, id) {
      return new Promise((esolve, reject) => {
        cancelSharingReportApi(id)
          .then(res => {
            if (res.code === 0) {
              esolve(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getReportUser ({ commit }, id) {
      return new Promise((resolve, reject) => {
        getReportUserApi(id)
          .then(res => {
            if (res.code === 0) {
              resolve(res.data)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getSharedKey({ commit }, reportId) {
      return new Promise((resolve, reject) => {
        getSharedKeyApi(reportId).then(res => {
          resolve(res)
        }).catch(err => {
          console.error(err)
          reject(err)
        })
      })
    },
    addSharedKey({ commit }, props) {
      return new Promise((resolve, reject) => {
        addSharedKeyApi(props.reportId, props.token).then(res => {
          if (res.code === 0){
            resolve(res.data)
          }else {
            reject(res.msg)
          }
        }).catch(err => {
          console.error(err)
          reject(err)
        })
      })
    },
    deleteSharedKey({ commit }, props) {
      return new Promise((resolve, reject) => {
        deleteSharedKeyApi(props).then(res => {
          if (res.code === 0){
            resolve(res.data)
          }else {
            reject(res.msg)
          }
        }).catch(err => {
          console.error(err)
          reject(err)
        })
      })
    },
    getFollowingReports() {
      return new Promise((resolve, reject) => {
        getFollowingReportsApi().then(res => {
          if (res.code === 0){
            resolve(res.data)
          }else {
            reject(res.msg)
          }
        }).catch(err => {
          console.error(err)
          reject(err)
        })
      })
    },
    getFollowingStatus({ commit },id) {
      return new Promise((resolve, reject) => {
        getFollowingStatusApi(id).then(res => {
          if (res.code === 0){
            resolve(res.data)
          }else {
            reject(res.msg)
          }
        }).catch(err => {
          console.error(err)
          reject(err)
        })
      })
    },
    followReport({ commit },id) {
      return new Promise((resolve, reject) => {
        followReportApi(id).then(res => {
          if (res.code === 0){
            resolve(res.data)
          }else {
            reject(res.msg)
          }
        }).catch(err => {
          console.error(err)
          reject(err)
        })
      })
    },
    unfollowReport({ commit },id) {
      return new Promise((resolve, reject) => {
        unfollowReportApi(id).then(res => {
          if (res.code === 0){
            resolve(res.data)
          }else {
            reject(res.msg)
          }
        }).catch(err => {
          console.error(err)
          reject(err)
        })
      })
    }
  },
  getters: {

  },
}

export default reportManage
